
import  numpy as np
import  sklearn.cluster as skc
from sklearn import metrics
import matplotlib.pyplot as plt

mac_id = dict()
online_time = []
f = open('TestData.txt',encoding='utf-8')
for line in f:
    mac = line.split(',')[2]
    online_time = int(line.split(',')[6])
    start_time = int(line.split(',')[4].split(' ')[1].split(':')[0])
    if mac not in mac_id:
        mac_id[mac] = len(online_time)
        online_time.append((start_time,online_time))
    else:
        online_time[mac_id[mac]] = [(start_time,online_time)]

real_X = np.array(online_time).reshape((-1,2))


X = real_X[:,0:1]

db = skc.DBSCAN(eps=0.01,min_samples=20).fit(X)
labels = db.labels_

print("Labels:")
print(labels)
ratio = len(labels[labels[:] == -1])/len(labels)
print("Noise ratio:",format(ratio,'.2&'))

n_clusters = len(set(labels)) - (1 if -1 in labels else 0)

print('Estimated number of clusters: %d' %n_clusters)
print('Silhouette Coefficient :%0.3f'%metrics.silhouette_score(X,labels))

for i in range(n_clusters):
    print('Cluster',i,':')
    print(list(X[labels == i].flatten()))
